/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interfaz;

import Interfaz.Validacion;
import Interfaz.Validacion;
import Negocio.Compras;
import Negocio.GerenteProveedor;
import Negocio.Lista_login;
import Negocio.Lista_productos;
import Negocio.Lista_proveedor;
import Negocio.Login;
import Negocio.Producto;
import Negocio.Proveedor;
import Negocio.UsuarioLogin;
import Negocio.Usuarios;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Jorge
 */
public class GerenteInterfaz extends javax.swing.JFrame {
    Usuarios usuario = new Usuarios();
    Login login = new Login();
    Proveedor proveedor = new Proveedor();
    GerenteProveedor gerProv = new GerenteProveedor();
    UsuarioLogin ul = new UsuarioLogin();
    Validacion validar = new Validacion();
    Producto producto = new Producto();
    Compras compra = new Compras();
    Lista_proveedor listaProveedor = new Lista_proveedor();
    Lista_login listaLogin = new Lista_login();
    Lista_productos  listaProductos = new Lista_productos();
    DefaultComboBoxModel comboProveedor = new DefaultComboBoxModel();
    
    /**
     * Creates new form GerenteInterfaz
     */
    public GerenteInterfaz() {
        initComponents();
        this.setTitle("GERENTE");
        
    }
    
    public void limpiarTabla(JTable tbl){
        int aux = tbl.getRowCount();
        do{
            try{
                DefaultTableModel temp = (DefaultTableModel) tbl.getModel();
                temp.removeRow(temp.getRowCount()-1);
            }
            catch(ArrayIndexOutOfBoundsException e){
            }
            aux--;
        }while(aux > 0);
    }

    private void actualizarproducto(){
        listaProductos.listar();
        ArrayList prods = new ArrayList();
        prods = listaProductos.getProductos();
        DefaultTableModel temp = (DefaultTableModel) tblProductosGerente.getModel();
        for (int i = tblProductosGerente.getRowCount() - 1; i >= 0; i--) {
            temp.removeRow(i);
        }
        for (int i = 0; i < prods.size(); i++) {
            Producto prod = new Producto();
            prod = (Producto) prods.get(i);
            proveedor.setId(prod.getProveedor().getId());
            proveedor.buscar();
            Object nuevo[] = {prod.getId(), prod.getNombre(), prod.getCantidad(), prod.getPrecio(), proveedor.getNombre()};
            temp.addRow(nuevo);
        }
        tblProductosGerente.setModel(temp);
    }
    
    public void agregarProveedoresCombo() {
        comboProveedor.removeAllElements();
        listaProveedor.listar();
        ArrayList proveedores = listaProveedor.getProveedor();
        Iterator it = proveedores.iterator();
        while (it.hasNext()) {
            Proveedor p = (Proveedor) it.next();
            comboProveedor.addElement(p.getNombre());
        }
        cmbProveedor.setModel(comboProveedor);
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel33 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        txtnombrepro = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        txtcantidadpro = new javax.swing.JTextField();
        btnagregarpro = new javax.swing.JButton();
        btnactualizarpro = new javax.swing.JButton();
        btnlimpiarpro = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblProductosGerente = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtPrecioGer = new javax.swing.JTextField();
        cmbProveedor = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        lblCedUs = new javax.swing.JLabel();
        lblNombreUs = new javax.swing.JLabel();
        menuGerente = new javax.swing.JMenuBar();
        jMenuGerente = new javax.swing.JMenu();
        itemGerente1 = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        itemGerente2 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel8.setText("Nombre:");

        txtnombrepro.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtnombreproKeyPressed(evt);
            }
        });

        jLabel9.setText("Cantidad:");

        txtcantidadpro.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtcantidadproKeyPressed(evt);
            }
        });

        btnagregarpro.setText("Comprar");
        btnagregarpro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnagregarproActionPerformed(evt);
            }
        });

        btnactualizarpro.setText("Actualizar");
        btnactualizarpro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnactualizarproActionPerformed(evt);
            }
        });

        btnlimpiarpro.setText("Limpiar");
        btnlimpiarpro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnlimpiarproActionPerformed(evt);
            }
        });

        tblProductosGerente.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Id", "Nombre", "Cantidad", "Precio", "Proveedor"
            }
        ));
        jScrollPane2.setViewportView(tblProductosGerente);

        jLabel1.setText("Proveedor:");

        jLabel3.setText("Precio:");

        cmbProveedor.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout jPanel33Layout = new javax.swing.GroupLayout(jPanel33);
        jPanel33.setLayout(jPanel33Layout);
        jPanel33Layout.setHorizontalGroup(
            jPanel33Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel33Layout.createSequentialGroup()
                .addGap(126, 126, 126)
                .addGroup(jPanel33Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9)
                    .addComponent(jLabel1))
                .addGap(10, 10, 10)
                .addGroup(jPanel33Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(txtPrecioGer, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                    .addComponent(txtcantidadpro, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtnombrepro, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cmbProveedor, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel33Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnlimpiarpro, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnactualizarpro, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnagregarpro, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(113, 113, 113))
            .addGroup(jPanel33Layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 521, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(76, Short.MAX_VALUE))
        );
        jPanel33Layout.setVerticalGroup(
            jPanel33Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel33Layout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addGroup(jPanel33Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnagregarpro)
                    .addGroup(jPanel33Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel8)
                        .addComponent(txtnombrepro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel33Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnactualizarpro)
                    .addComponent(jLabel9)
                    .addComponent(txtcantidadpro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel33Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnlimpiarpro)
                    .addComponent(jLabel3)
                    .addComponent(txtPrecioGer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel33Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cmbProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(49, 49, 49)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(31, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Comprar Productos", jPanel33);

        jLabel2.setText("Usuario:");

        lblCedUs.setText("0103849873");

        lblNombreUs.setText("jLabel4");

        jMenuGerente.setText("File");

        itemGerente1.setText("Cerrar Sesion");
        itemGerente1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemGerente1ActionPerformed(evt);
            }
        });
        jMenuGerente.add(itemGerente1);
        jMenuGerente.add(jSeparator1);

        itemGerente2.setText("Salir");
        itemGerente2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemGerente2ActionPerformed(evt);
            }
        });
        jMenuGerente.add(itemGerente2);

        menuGerente.add(jMenuGerente);

        setJMenuBar(menuGerente);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblCedUs)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblNombreUs, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lblCedUs)
                    .addComponent(lblNombreUs))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 411, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void itemGerente2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemGerente2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_itemGerente2ActionPerformed

    private void itemGerente1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemGerente1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_itemGerente1ActionPerformed

    private void btnlimpiarproActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnlimpiarproActionPerformed
        txtnombrepro.setText("");
        txtcantidadpro.setText("");
        limpiarTabla(tblProductosGerente);
    }//GEN-LAST:event_btnlimpiarproActionPerformed

    private void btnactualizarproActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnactualizarproActionPerformed
        // TODO add your handling code here:
        actualizarproducto();
    }//GEN-LAST:event_btnactualizarproActionPerformed

    private void btnagregarproActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnagregarproActionPerformed
        try{
            producto.setNombre(txtnombrepro.getText());
            producto.setCantidad(Integer.parseInt(txtcantidadpro.getText()));
            producto.setPrecio(Double.parseDouble(txtPrecioGer.getText()));            
            proveedor.setNombre(cmbProveedor.getSelectedItem().toString());
            proveedor.buscarNombre();
            producto.setProveedor(proveedor);
            usuario.setCedula(lblCedUs.getText());
            
            producto.grabar(); 
            
            gerProv.setProveedor(proveedor);
            gerProv.setUsuario(usuario); 
            
            gerProv.grabar();
        }
        catch(NumberFormatException e){
            JOptionPane.showMessageDialog(null, "Faltan Datos");
        }
    }//GEN-LAST:event_btnagregarproActionPerformed

    private void txtcantidadproKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtcantidadproKeyPressed
        // TODO add your handling code here:
        validar.ValidarNumeros(txtcantidadpro);
    }//GEN-LAST:event_txtcantidadproKeyPressed

    private void txtnombreproKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtnombreproKeyPressed
        // TODO add your handling code here:
        validar.ValidarPalabras(txtnombrepro);
    }//GEN-LAST:event_txtnombreproKeyPressed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        agregarProveedoresCombo();
        login.setId(listaLogin.getId());
        ul.setLogin(login);
        ul.buscarId();        
        usuario.setCedula(ul.getUsuario().getCedula());
        usuario.buscar();
        lblCedUs.setText(usuario.getCedula());
        lblNombreUs.setText(usuario.getNombre()+" "+usuario.getApellido());
    }//GEN-LAST:event_formWindowOpened

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GerenteInterfaz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GerenteInterfaz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GerenteInterfaz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GerenteInterfaz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GerenteInterfaz().setVisible(false);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnactualizarpro;
    private javax.swing.JButton btnagregarpro;
    private javax.swing.JButton btnlimpiarpro;
    private javax.swing.JComboBox cmbProveedor;
    private javax.swing.JMenuItem itemGerente1;
    private javax.swing.JMenuItem itemGerente2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenuGerente;
    private javax.swing.JPanel jPanel33;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JLabel lblCedUs;
    private javax.swing.JLabel lblNombreUs;
    private javax.swing.JMenuBar menuGerente;
    private javax.swing.JTable tblProductosGerente;
    private javax.swing.JTextField txtPrecioGer;
    private javax.swing.JTextField txtcantidadpro;
    private javax.swing.JTextField txtnombrepro;
    // End of variables declaration//GEN-END:variables
}
